﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TM.Data;
using TM.Data.Contracts;
using TM.Data.ViewModels.VmMapper;
using TM.Plugable;

namespace ProjectHost
{
    public partial class ProjectEditorScreen : Form, IProjectEditor
    {
        public ProjectEditorScreen()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            projectEditorControl.ProjectDataIsChanged += new ProjectDataIsChangedHandler(projectEditorControl_ProjectDataIsChanged);
        }

        void projectEditorControl_ProjectDataIsChanged(object sender, bool isDirty)
        {
            InvokeDataIsChanged(sender, isDirty);
        }

        private void InvokeDataIsChanged(object sender, bool isDirty)
        {
            if(ProjectDataIsChanged!=null)
            {
                ProjectDataIsChanged.Invoke(sender, isDirty);
            }
        }

        #region Implementation of IProjectEditor

        public event ProjectDataIsChangedHandler ProjectDataIsChanged;

        public IVmMapper VmMapper
        {
            get
            {
                return projectEditorControl.VmMapper;
            }
        }

        public IUnitOfWork UoW
        {
            get
            {
                return projectEditorControl.UoW;
            }
        }

        public bool IsDataLoaded { get { return projectEditorControl.IsDataLoaded; } }
        
        public bool IsDirty { get { return projectEditorControl.IsDirty; } }

        public object Session { get { return projectEditorControl.Session; } }
        public Guid ProjectId { get { return projectEditorControl.ProjectId; } }

        public void SetIoC(IIoCParams ioCParams, IVmMapper vmMapper)
        {
            projectEditorControl.SetIoC(ioCParams, vmMapper);
        }

        public void LoadProjectInformation(Guid projectId)
        {
            projectEditorControl.LoadProjectInformation(projectId);
        }

        public void RemoveCurrentTask()
        {
            projectEditorControl.RemoveCurrentTask();
        }

        public void CreateNewTask()
        {
            projectEditorControl.CreateNewTask();
        }

        public void Save()
        {
            if (projectEditorControl.IsDirty)
            {
                projectEditorControl.Save();    
            }
        }

        public void Release()
        {
            projectEditorControl.Release();
        }

        #endregion
    }
}
